package com.example.laboratory.root_ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import com.example.laboratory.R;

import com.example.laboratory.ui.adapter.ItemCheckDialogAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Created by lzl on 2017/6/17.
 * @function: 自定义单选列表dialog
 * @description:
 */

public class ProItemListDialog extends Dialog{


    private Context mContext;

    private Button  mCloseImg;//关闭按钮
    private Button mSureBtn;//确定选择按钮
    private ListView mListView;//列表
    private List<String> mList;
    private SparseBooleanArray stateCheckedMap = new SparseBooleanArray();//用来存放CheckBox的选中状态，true为选中,false为没有选中
    private ItemCheckDialogAdapter itemCheckDialogAdapter;
    private List<String> mCheckedData = new ArrayList<>();//将选中数据放入里面

    private int posi;
    /**
     * 记录当前选中的值
     */
    private String currentSelectItemName;
    /**
     * 是否被点击了 如果被点击了 则此时可以点击确定按钮
     */
    private boolean isBeClick=false;
    public ProItemListDialog(Context context, List<String> list) {
        super(context, R.style.dialog);
        this.mContext=context;
        this.mList=list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pro_item_selected);
           //点击外部区域不可取消
        setCanceledOnTouchOutside(false);
        initView();
        itemCheckDialogAdapter=new ItemCheckDialogAdapter(mContext,mList,stateCheckedMap);
        mListView.setAdapter(itemCheckDialogAdapter);
        setOnListViewItemClickListener();
        initEvent();
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    }


    private void initView() {
        mCloseImg=  (Button) findViewById(R.id.single_select_cancel);
        mSureBtn= (Button) findViewById(R.id.single_select_sure_btn);
        mListView= (ListView) findViewById(R.id.checkbox_list);
        TextView dialog_title=findViewById(R.id.dialog_title);
        dialog_title.setText("实验室");
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = AbsListView.LayoutParams.MATCH_PARENT;
        lp.y = 0;//设置Dialog距离底部的距离
        dialogWindow.setAttributes(lp);

    }

    private void initEvent() {
        mCloseImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mSureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentSelectItemName!=null&&!currentSelectItemName.equals("")){
                    dismiss();
                }else {
                    Toast.makeText(mContext,"您当前未选择任何项",Toast.LENGTH_SHORT).show();
                }

            }
        });
//
//        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                if(mList!=null){
//                    currentSelectItemName=mList.get(position);
//                    posi=position;
//                }
//            }
//        });
    }


    private void setOnListViewItemClickListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                updateCheckBoxStatus(view, position);
            }
        });
    }

    private void updateCheckBoxStatus(View view, int position) {
        ItemCheckDialogAdapter.ViewHolder holder = (ItemCheckDialogAdapter.ViewHolder) view.getTag();
        holder.checkBox.toggle();//反转CheckBox的选中状态

        mListView.setItemChecked(position, holder.checkBox.isChecked());//长按ListView时选中按的那一项
        stateCheckedMap.put(position, holder.checkBox.isChecked());//存放CheckBox的选中状态
         if (holder.checkBox.isChecked()) {
             mCheckedData.add(mList.get(position));//CheckBox选中时，把这一项的数据加到选中数据列表
                  } else {
             mCheckedData.remove(mList.get(position));
             //CheckBox未选中时，把这一项的数据从选中数据列表移除
             }
         itemCheckDialogAdapter.notifyDataSetChanged();
    }




}